<template>
  <div>
    <!-- 搜索标题 -->
    <van-cell title="搜索历史">
      <div>
        <van-icon @click="isShowDelete = true" v-if="!isShowDelete" name="delete-o" />
        <div v-else class="edit">
          <span @click="searchHistory.splice(0)" style="margin-right: 10px;">全部删除</span>
          <span @click="isShowDelete = false">完成</span>
        </div>
      </div>
    </van-cell>

    <!-- 搜索历史内容 -->
    <van-cell @click="clickHistory(index, item)" :title="item" v-for="(item, index) in searchHistory" :key="item">
      <van-icon v-if="isShowDelete" name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShowDelete: false
    }
  },

  props: {
    // 接收搜索历史
    searchHistory: {
      type: Array,
      required: true
    }
  },

  created() {

  },

  methods: {
    // 点击了搜索历史
    clickHistory(i, item) {
      if (this.isShowDelete) {
        // 处于删除状态
        console.log('删除')
        // 复杂数据类型不允许直接赋值,但是允许修改
        this.searchHistory.splice(i, 1)
      } else {
        // 进行搜索
        this.$emit('on-search', item)
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
